package com.panfeng.xcloud.boss.provider.member.config;

import com.panfeng.xcloud.common.core.configuration.DataDictionaryConfig;
import com.panfeng.xcloud.common.core.constants.RedisConstants;
import com.panfeng.xcloud.common.core.enums.DataDictionaryTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ClearCacheDataRunner implements CommandLineRunner {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

/*    @Resource
    private DataDictionaryMapperExt dataDictionaryMapperExt;*/

    @Autowired
    private DataDictionaryConfig dataDictionaryConfig;

    @Override
    public void run(String... args) throws Exception {
        log.info(">>>>清除缓存重新冲数据库中加载");
        //防止redis挂掉造成uid的顺序错乱
        String cacheCurrentLastNum = stringRedisTemplate.opsForValue().get(RedisConstants.UID_CURRENTLASTNUM_PREFIX);
        if (StringUtils.isEmpty(cacheCurrentLastNum)) {
            //String currentCurrentLastNum = dataDictionaryMapperExt.queryValueByKey(DataDictionaryTypeEnum.FIRSTNUM.getCode());
            String currentCurrentLastNum = dataDictionaryConfig.getCodeToNameMap().get(DataDictionaryTypeEnum.firstNum.getCode());
            stringRedisTemplate.opsForValue().set(RedisConstants.UID_CURRENTLASTNUM_PREFIX, currentCurrentLastNum);
        }
    }

}
